package com.chinasoftinc.facommon.utils;

import ohos.rpc.IRemoteObject;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.rpc.RemoteException;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 远程FA服务响应PA工具类
 *
 * @since 2022-05-13
 */
public class RemoteUtils {

    private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor();
    private static final String TAG = "RemoteUtils";

    private static final Response OK = new Response(0);

    public static class Response {
        private int code;
        private String msg;
        private Object data;

        public Response(int code) {
            this.code = code;
        }

        public Response setCode(int code) {
            this.code = code;
            return this;
        }

        public Response withMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public Response withData(Object data) {
            this.data = data;
            return this;
        }

        public String toJson() {
            return ZSONObject.toZSONString(this);
        }
    }

    public static boolean responseSuccess(MessageParcel reply) {
        try {
            reply.writeString(OK.toJson());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void responseError(MessageParcel reply,int code,String msg) {
        try {
            Response response = new Response(code).withMsg(msg);
            reply.writeString(response.toJson());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Map<String, Object> genData(String key, Object value) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(key, value);
        return map;
    }

    public static void sendRemoveMessage(IRemoteObject remoteObject, int code, String key, Object value, long delay) {
        sendRemoveMessage(remoteObject, code, genData(key, value), delay);
    }

    public static void sendRemoveMessage(IRemoteObject remoteObject, int code, Map<String, Object> dataMap, long delay) {
        if (remoteObject == null) {
            return;
        }
        EXECUTOR_SERVICE.schedule(() -> {
            MessageParcel data = MessageParcel.obtain();
            MessageParcel reply = MessageParcel.obtain();
            MessageOption option = new MessageOption();
            if (dataMap != null) {
                data.writeString(ZSONObject.toZSONString(dataMap));
            }
            try {
                LogUtil.info(TAG, "sendRemoveMessage ：" + ZSONObject.toZSONString(data));
                remoteObject.sendRequest(code, data, reply, option);
            } catch (RemoteException e) {
                LogUtil.error(TAG, "sendRemoveMessage error");
            }
            reply.reclaim();
            data.reclaim();
        }, delay, TimeUnit.MILLISECONDS);
    }

}
